package com.hpin.fansea.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.adapter.CityListAdapter;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.ChooseCity;
import com.hpin.fansea.bean.City;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择城市页面
 * Created by Aaron on 15/9/8.
 */
public class FirstCitySelectActivty extends BaseActivity {

    private ListView lv_city;
    private CityListAdapter cityListAdapter;
    private List<City> cityArray = new ArrayList<City>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_city_select);

        mApplication.addActivity(this);

        initView();

        initCityData();
    }

    /**
     * 加载城市列表
     */
    private void initCityData() {

        HttpUtil.getInstance().setGetRequest("获取中...", this, Constant.WEB_ROOT + Constant.CITY_LIST, new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {

                    ChooseCity chooseCity = JSONObject.parseObject(str, ChooseCity.class);

                    if (Constant.STATUS_SUCESS.equals(chooseCity.status)) {

                        cityArray = chooseCity.result;
                        cityListAdapter.setDate(cityArray);
                        cityListAdapter.notifyDataSetChanged();

                    }else {
                        showToast(chooseCity.message);
                    }

                }catch (Exception e) {
                    e.printStackTrace();
                    showToast("获取城市列表失败");
                }

            }
        }, null);

    }

    private void initView() {

        TextView tv_left = (TextView) findViewById(R.id.tv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.city));

        lv_city = (ListView) findViewById(R.id.lv_city_first);
        cityListAdapter = new CityListAdapter(this, cityArray);
        lv_city.setAdapter(cityListAdapter);

        lv_city.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                String cityId = cityArray.get(arg2).cityId;
                String cityName = cityArray.get(arg2).cityName;
                Intent intent = new Intent(FirstCitySelectActivty.this, FirstCommunitySelectActivity.class);
                intent.putExtra("cityId", cityId);
                intent.putExtra("cityName", cityName);

                startActivity(intent);
                finish();
            }
        });

        tv_left.setOnClickListener(this);

    }

    @Override
    public void widgetClick(View v) {


        switch (v.getId()) {
            case R.id.tv_left:
                finish();
                break;
        }
    }

}
